package com.mdlib.helper.ui.fragments.rush.order;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.core.util.EmptyUtils;
import com.ljlib.core.util.common.PhoneUtils;
import com.ljlib.core.util.common.TimeUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.common.CommonAdapter;
import com.mdlib.helper.common.CommonViewHolder;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.OrderDetailsInfo;
import com.mdlib.helper.model.entity.OrderEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.adapter.OrderDetailsAdapter;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/28 13:24
 * 邮箱：hqy_xz@126.com
 */

public class RushOrderDetailsFragment extends BaseTitleFragment {


    @Bind(R.id.iv_rush_details_goods_bg)
    ImageView mIvRushDetailsGoodsBg;
    @Bind(R.id.tv_rush_details_task)
    TextView mTvRushDetailsTask;
    @Bind(R.id.tv_rush_details_type)
    TextView mTvRushDetailsType;
    @Bind(R.id.tv_rush_details_goods_name)
    TextView mTvRushDetailsGoodsName;
    @Bind(R.id.rv_rush_details_pic)
    GridView mRvRushDetailsPic;
    @Bind(R.id.iv_rush_details_line)
    ImageView mIvRushDetailsLine;
    @Bind(R.id.tv_rush_details_time)
    TextView mTvRushDetailsTime;
    @Bind(R.id.tv_rush_details_address)
    TextView mTvRushDetailsAddress;
    @Bind(R.id.tv_rush_details_addresses)
    TextView mTvRushDetailsAddresses;
    @Bind(R.id.tv_rush_details_goods_phone)
    TextView mTvRushDetailsGoodsPhone;
    @Bind(R.id.tv_rush_details_dervice_money)
    TextView mTvRushDetailsDerviceMoney;
    @Bind(R.id.ll_rush_service_money)
    LinearLayout mLlRushServiceMoney;
    @Bind(R.id.tv_rush_details_btn)
    TextView mTvRushDetailsBtn;
    @Bind(R.id.iv_rush_details_type)
    ImageView mIvRushDetailsType;
    @Bind(R.id.tv_rush_details_goods_money)
    TextView mTvRushDetailsGoodsMoney;
    @Bind(R.id.ll_rush_goods_money)
    LinearLayout mLlRushGoodsMoney;
    @Bind(R.id.tv_rush_details_purchase)
    TextView mTvRushDetailsPurchase;
    @Bind(R.id.ll_rush_details_btn)
    LinearLayout mLlRushDetailsBtn;
    private List<String> imageList = new ArrayList<>();
    private OrderDetailsAdapter mOrderDetailsAdapter;
    private String busId;
    private String role = "1";
    private OrderEntity order;
    CommonAdapter<String> commonAdapter;

    public static RushOrderDetailsFragment newInstanct(String busId) {
        RushOrderDetailsFragment fragment = new RushOrderDetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(UIHelper.BUS_ID, busId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EmptyUtils.isEmpty(getArguments())) {
            getActivity().finish();
        }
        busId = getArguments().getString(UIHelper.BUS_ID);

    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_rush_order_details;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.details_order_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        init();

    }

    public void init() {
        if (AccountModel.getInstance().getRoleType().equals("user")) {
            role = "1";
        } else {
            role = "2";
        }
        mOrderDetailsAdapter = new OrderDetailsAdapter(imageList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        commonAdapter = new CommonAdapter<String>(getActivity(), imageList, R.layout.item_order_details) {
            @Override
            public void convert(CommonViewHolder holder, String s, final int position) {
                final String url = "http://www." + s;
                holder.setGImage(R.id.iv_order_details_pic, url);
                holder.setOnClickListener(R.id.iv_order_details_pic, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UIHelper.showPicZoom(getActivity(), url);
                    }
                });
            }
        };
        mRvRushDetailsPic.setAdapter(commonAdapter);
//        mRvRushDetailsPic.setLayoutManager(linearLayoutManager);
//        mRvRushDetailsPic.setAdapter(mOrderDetailsAdapter);
//        mRvRushDetailsPic.addOnItemTouchListener(new OnItemClickListener() {
//            @Override
//            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
//
//            }
//
//            @Override
//            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                super.onItemChildClick(adapter, view, position);
//                UIHelper.showPicZoom(getActivity(), imageList.get(position));
//            }
//        });
        getOrderDetails();
    }

    @OnClick({R.id.tv_rush_details_btn, R.id.tv_rush_details_purchase})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_rush_details_btn:
                if (EmptyUtils.isEmpty(order.getGetPhone())) {
                    ToastUtil.showToast("下单人没有填写手机号");
                    return;
                }
                PhoneUtils.call(getActivity(), order.getGetPhone());
                break;
            case R.id.tv_rush_details_purchase:
                setImmediatelyBuy();
                break;
        }
    }

    /**
     * 跑客确认立即购买
     */
    private void setImmediatelyBuy() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .setImmediatelyBuy(busId + "", "true")
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        getOrderDetails();
                        ToastUtil.showToast(baseResult.msg);
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));
    }

    /**
     * 跑客订单详情
     */
    private void getOrderDetails() {

        addSubscribe(DataManager.getInstance().getUserApi()
                .getOrderDetaile(busId + "", role)
                .compose(RxHelper.<OrderEntity>defaultHandleResult())
                .subscribe(new RxSubscribe<OrderEntity>() {
                    @Override
                    protected void onSucc(OrderEntity orderEntity) {
                        upOrderDetails(orderEntity);
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));
    }


    /**
     * 更新订单详情
     *
     * @param info
     */
    private void upOrderDetails(OrderEntity info) {
        order = info;
        imageList = info.getPicPath();
        commonAdapter.refresh(imageList);
//        mOrderDetailsAdapter.setNewData(imageList);
//        mOrderDetailsAdapter.notifyDataSetChanged();
        if (info.getOrderStatus() >= 3) {
            mLlRushGoodsMoney.setVisibility(View.VISIBLE);
            if (info.getOfflinePayGoods().equals("false")) {
                mTvRushDetailsGoodsMoney.setText("￥" + info.getSpendMoney());
            } else {
                mTvRushDetailsGoodsMoney.setText("线下支付");
            }
        }
        if (info.getOrderStatus() == 1) {
            mTvRushDetailsType.setText(getActivity().getResources().getString(R.string.generic_order_type_one));
        } else if (info.getOrderStatus() == 2) {
            mTvRushDetailsType.setText(getActivity().getResources().getString(R.string.generic_run_order_type_two));
            mLlRushDetailsBtn.setVisibility(View.VISIBLE);
        } else if (info.getOrderStatus() == 3) {
            if (info.getOfflinePayGoods().equals("false")) {
                mTvRushDetailsType.setText(getActivity().getResources().getString(R.string.generic_run_order_type_three));
            } else {
                mTvRushDetailsType.setText("线下支付");
            }
            mLlRushDetailsBtn.setVisibility(View.GONE);
        } else if (info.getOrderStatus() == 4) {
            mTvRushDetailsType.setText(getActivity().getResources().getString(R.string.generic_run_order_type_four));
            mLlRushDetailsBtn.setVisibility(View.GONE);
        } else if (info.getOrderStatus() == 5) {
            mTvRushDetailsType.setText(getActivity().getResources().getString(R.string.generic_run_order_type_five));
        } else if (info.getOrderStatus() == 6) {
            mLlRushDetailsBtn.setVisibility(View.GONE);
            mTvRushDetailsType.setText(getActivity().getResources().getString(R.string.generic_order_type_six));
            mTvRushDetailsType.setTextColor(getActivity().getResources().getColor(R.color.generic_view_bg));
        } else if (info.getOrderStatus() == 7) {
            mLlRushDetailsBtn.setVisibility(View.GONE);
            mTvRushDetailsType.setText(getActivity().getResources().getString(R.string.generic_order_type_seven));
            mTvRushDetailsType.setTextColor(getActivity().getResources().getColor(R.color.generic_view_bg));
        } else if (info.getOrderStatus() == 8) {
            mLlRushDetailsBtn.setVisibility(View.GONE);
            mTvRushDetailsType.setText(getActivity().getResources().getString(R.string.generic_order_type_eight));
        } else if (info.getOrderStatus() == 9) {
            mLlRushDetailsBtn.setVisibility(View.GONE);
            mTvRushDetailsType.setText(getActivity().getResources().getString(R.string.generic_order_type_night));
        }
        if (info.getHopeTime() == 0) {
            mTvRushDetailsTime.setText(getActivity().getString(R.string.generic_order_success_time));
        } else {
            mTvRushDetailsTime.setText(TimeUtils.milliseconds2String(Long.valueOf(info.getHopeTime()) * 1000, TimeUtils.SUCCESS_MONTH));
        }
        if (info.getBusType() == 1) {
            Glide.with(getActivity())
                    .load(R.drawable.iv_details_agecy)
                    .into(mIvRushDetailsType);

            Drawable agencys = getActivity().getResources().getDrawable(R.drawable.vicinity_order_class);
            agencys.setBounds(0, 0, agencys.getMinimumWidth(), agencys.getMinimumHeight());
            mTvRushDetailsAddress.setText(info.getHandleType());
        } else if (info.getBusType() == 2) {
            Glide.with(getActivity())
                    .load(R.drawable.iv_details_minnetonka)
                    .into(mIvRushDetailsType);
            Drawable minn = getActivity().getResources().getDrawable(R.drawable.vicinity_order_shop);
            minn.setBounds(0, 0, minn.getMinimumWidth(), minn.getMinimumHeight());
            mTvRushDetailsAddress.setCompoundDrawables(minn, null, null, null);
            mTvRushDetailsAddress.setText(info.getPostAddr());
        } else if (info.getBusType() == 3) {
            Glide.with(getActivity())
                    .load(R.drawable.iv_details_take)
                    .into(mIvRushDetailsType);
            Drawable take = getActivity().getResources().getDrawable(R.mipmap.vicinity_order_take);
            take.setBounds(0, 0, take.getMinimumWidth(), take.getMinimumHeight());
            mTvRushDetailsAddress.setCompoundDrawables(take, null, null, null);
            mTvRushDetailsAddress.setText(info.getPostAddr());
        }
        mTvRushDetailsAddresses.setText(info.getGetAddr());
        mTvRushDetailsGoodsPhone.setText("收货人手机号 " + info.getGetPhone());
        mTvRushDetailsDerviceMoney.setText("￥" + info.getServiceMoney());
        mTvRushDetailsGoodsName.setText(info.getRemark());

    }


}
